import axios from 'axios'
import store from '../store'

let paramsConvert = {}
paramsConvert.install = function (Vue, options) {
  Vue.paramsConvert = Vue.prototype.$paramsConvert = function (obj) {
      var params = new URLSearchParams()
      Object.keys(obj).forEach((key) => {
        params.append(key, obj[key])
      })
    return params
  }

  // 组织
  Vue.connErmsg = Vue.prototype.$connErmsg = function (items) {
    let str = ''
    items.forEach((item, index) => {
      // console.log(index);
      str += `${item.msg}${((index === items.length - 1) ? '': ',')}`
    })
    return str
  }


  // 清空对象数据
  Vue.clearObj = Vue.prototype.$clearObj = function (obj) {
    Object.keys(obj).forEach((key) => {
      obj[key] = ''
    })
  }

  // 刷新用户会员状态
  Vue.refreshVip = Vue.prototype.$refreshVip = function () {
    axios.post('/mscs/user/findUserType', this.$paramsConvert({userId: store.state.user.user.user.userId})).then((res) => {
      if (res.data.code === 1) {
        store.dispatch('upUserType', {userType: res.data.data.userType})
      }
    })
  }
}
export default paramsConvert
